import { _decorator, Component, js, Label, misc, ProgressBar, Sprite, SpriteAtlas } from 'cc'
import { Browser } from '../Base/Browser'
import EventManager from '../Runtime/EventManager'
import { GAME_STATE_ENUM } from '../Config/Enums/EnumsIndex'

const { ccclass, property } = _decorator

/**
 * @Description: 登录时展示状态和进度的UI
 * @Author: 李清
 * @Date: 2022-08-16 17:51:34
 */
@ccclass
export default class TipProgressUI extends Component {
  /** 提示 */
  @property(Label)
  public tipLabel: Label = null

  /** 进度 */
  @property(Label)
  public progressLabel: Label = null

  /** 进度 */
  @property(ProgressBar)
  public progressBar: ProgressBar = null

  @property(Sprite)
  public progressBgSpr: Sprite = null

  @property(Sprite)
  public progressSpr: Sprite = null

  /** 版本信息 */
  @property(Label)
  public versionLabel: Label = null

  @property(SpriteAtlas)
  private atlas: SpriteAtlas = null

  /** 首次加载资源提示 */
  @property(Label)
  public tipLabel2: Label = null

  /** 当前展示的进度 */
  private curProgress = 0

  /** 目标进度 */
  private targetProgress = 0

  /** 进度差值速度 */
  private progressLerpSpeed = 0.08

  /** 进度int值 */
  private progressIntValue = 0

  onLoad() {
    if (Browser.onMiniGame || Browser.isSameToMiniGame) {
      //小游戏不做赛季特殊处理
    } else {
      // const seasonId = SeasonMgr.isInSeason() ? SeasonMgr.getSeasonId(false) : 0
      // let jdImage1 = seasonId > 0 ? `jd1${seasonId}` : 'jd1'
      // let jdImage2 = seasonId > 0 ? `jd2${seasonId}` : 'jd2'
      // if (seasonId > 0 && SeasonMgr.isInSeasonOther()) {
      //   jdImage1 += '_1'
      //   jdImage2 += '_1'
      // }
      // this.progressBgSpr.spriteFrame = this.atlas.getSpriteFrame(jdImage1)
      // this.progressSpr.spriteFrame = this.atlas.getSpriteFrame(jdImage2)
    }
    EventManager.Instance.on(
      GAME_STATE_ENUM.ShowTipOrProgress,
      (args: { tip?: string; progress?: number; updateImmediately?: boolean; noRollBack?: boolean }) => {
        // console.error(`ShowTipOrProgress ${JSON.stringify(args)}`);
        // if (PlatformMgr.isFirstLoginGame()) {
        //   this.tipLabel2.node.active = !Browser.onAppNative
        // }
        if (args) {
          if (args.tip) {
            this.tipLabel.node.active = true
            this.tipLabel.string = args.tip
          } else {
            this.tipLabel.node.active = false
          }

          if (Number(args.progress)) {
            this.progressBar.node.active = true
            if (args.noRollBack == true) {
              if (this.targetProgress > args.progress) {
                console.log(
                  `Skip set target progress ${args.progress} because current target progress is ${this.targetProgress} and noRollBack is true`,
                )
                return
              }
            }
            this.targetProgress = args.progress
            if (!isNaN(this.curProgress) && args.updateImmediately) {
              this.curProgress = this.targetProgress
              this.progressBar.progress = this.curProgress
              this.progressIntValue = Math.round(this.curProgress * 100)
              this.progressLabel.string = `${this.progressIntValue}%`
            } else {
              this.update()
            }
          } else {
            this.progressBar.node.active = false
          }
        }
      },
    )
    this.tipLabel.node.active = false
    this.progressBar.node.active = false
    this.tipLabel2.node.active = false

    // EventManager.Instance.on(EventTypeEnum.ShowVersionInfo, (args: { versionInfo: string }) => {
    //   this.versionLabel.string = args.versionInfo
    // })
  }

  update() {
    if (this.curProgress !== this.targetProgress) {
      if (this.targetProgress < this.curProgress) {
        this.curProgress = this.targetProgress
      } else {
        // this.curProgress = misc.lerp(this.curProgress, this.targetProgress, this.progressLerpSpeed)
        if (Math.abs(this.curProgress - this.targetProgress) < 0.001) this.curProgress = this.targetProgress
      }
      this.progressBar.progress = this.curProgress

      if (isNaN(this.curProgress)) {
        this.curProgress = 0
        this.targetProgress = 0
        return
      }

      const intValue = Math.round(this.curProgress * 100)
      if (this.progressIntValue != intValue) {
        this.progressIntValue = intValue
        this.progressLabel.string = `${this.progressIntValue}%`
      }
    }
  }
}
